package com.ai.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.h2.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.service.ForumSorterReplyService;
import com.ai.web.vo.ForumSorterReplyV;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;
/**
 * 清分机使用交流区
 * 回复帖子
* @ClassName: ForumSorterReplyController 
* @Description: TODO 
* @author:  
* @date: 2014年3月24日
*
 */
@Controller
@RequestMapping("/forumSorterReply")
public class ForumSorterReplyController extends BaseController {
    

    private static Logger logger = Logger.getLogger(ForumSorterReplyController.class);
    
    @Autowired
    private ForumSorterReplyService forumSorterReplyService;
    
    @RequestMapping(value = "/list")
    public String list() {
        
        logger.info("hello......test");
        return "forumSorterReply/forumSorterReply_list";
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        QueryData qd = forumSorterReplyService.queryListAndCount(conditionMap);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<ForumSorterReplyV> dataList = new ArrayList<ForumSorterReplyV>();
        for(Object object:qd.getDataList()){
        	ForumSorterReplyV forumSorterReplyV = (ForumSorterReplyV)object;
        	if(forumSorterReplyV.getPostDate() != null){
        		forumSorterReplyV.setPostDateStr(sdf.format(forumSorterReplyV.getPostDate()));
        	}
            dataList.add(forumSorterReplyV);
        }
        qd.setDataList(dataList);
        return qd;
    }
    
    @RequestMapping(value = "/new")
    public String newforumSorterReply(Model model) {
        
        ForumSorterReplyV forumSorterReplyV = new ForumSorterReplyV();
        model.addAttribute(forumSorterReplyV);
        
        return "forumSorterReply/forumSorterReply_template";
    }
    
    @RequestMapping(value = "/update/{id}")
    public String updateforumSorterReply(@PathVariable("id") int id, Model model)
            throws Exception {
   	 	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ForumSorterReplyV forumSorterReplyV = forumSorterReplyService.getForumSorterReply(id);
    	if(forumSorterReplyV.getPostDate() != null){
    		forumSorterReplyV.setPostDateStr(sdf.format(forumSorterReplyV.getPostDate()));
    	}
        logger.info("----- getUser:" + forumSorterReplyV);
        
        model.addAttribute(forumSorterReplyV);
        
        return "forumSorterReply/forumSorterReply_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel saveforumSorterReply(ForumSorterReplyV forumSorterReplyV) throws Exception {
        
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("-----save:" + forumSorterReplyV);
        if(!StringUtils.isNullOrEmpty(forumSorterReplyV.getPostDateStr())){
        	forumSorterReplyV.setPostDate(sdf.parse(forumSorterReplyV.getPostDateStr()));
        }
        
        ResultModel rm = forumSorterReplyService.saveForumSorterReply(forumSorterReplyV);
        
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deleteforumSorterReply(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = forumSorterReplyService.deleteForumSorterReplys(userIds);
        
        return rm;
    }
    
    
}
